#include <func.h>

void cleanup(void* p)
{
    printf("i am cleanup func %ld\n",(long)p);
}
void* pthread_func(void* p)
{
    pthread_cleanup_push(cleanup,(void*)1);
    pthread_cleanup_push(cleanup,(void*)2);
    sleep(1);
    pthread_cleanup_pop(1);
    pthread_cleanup_pop(1);//pop[1]弹出清理函数并执行，pop[0]弹出清理函数不执行
    return NULL;
}
int main()
{
    pthread_t pthid;
    int ret=pthread_create(&pthid,NULL,pthread_func,NULL);
    THREAD_ERROR_CHECK(ret,"pthread_create")
    //cancel时子线程并没有开始free对应的空间
    ret=pthread_cancel(pthid);
    THREAD_ERROR_CHECK(ret,"pthread_cancel")
    ret=pthread_join(pthid,NULL);
    THREAD_ERROR_CHECK(ret,"pthread_join")
    printf("i am main thread\n");
    return 0;
}